﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Xml;
using BLL;
using AjaxControlToolkit;

public partial class left : System.Web.UI.UserControl
{
    BLL.Business.BookBLL bookBLL = new BLL.Business.BookBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindBigType();
            BindStar();
            
        }
    }
    // 星级评分绑定
    public void BindStar()
    {
        Ratingrecord rr = new Ratingrecord();
        int SellerAttiTotal = 0;
        int SellerAttiOne = Int32.Parse(rr.Count("服务态度", "1"));
        SellerAttiTotal = SellerAttiTotal + SellerAttiOne;

        int SellerAttiTwo = Int32.Parse(rr.Count("服务态度", "2"));
        SellerAttiTotal = SellerAttiTotal + SellerAttiTwo;

        int SellerAttiThr = Int32.Parse(rr.Count("服务态度", "3"));
        SellerAttiTotal = SellerAttiTotal + SellerAttiThr;

        int SellerAttiFor = Int32.Parse(rr.Count("服务态度", "4"));
        SellerAttiTotal = SellerAttiTotal + SellerAttiFor ;

        int SellerAttiFiv = Int32.Parse(rr.Count("服务态度", "5"));
        SellerAttiTotal = SellerAttiTotal + SellerAttiFiv;
        double SellerAttiScore = (double)(SellerAttiOne * 1 + SellerAttiTwo * 2 + SellerAttiThr * 3 + SellerAttiFor * 4 + SellerAttiFiv * 5) / SellerAttiTotal;

        int ConsistentProTotal = 0;
        int ConsistentProOne = Int32.Parse(rr.Count("宝贝相符", "1"));
        ConsistentProTotal = ConsistentProTotal + ConsistentProOne;

        int ConsistentProTwo = Int32.Parse(rr.Count("宝贝相符", "2"));
        ConsistentProTotal = ConsistentProTotal + ConsistentProTwo;

        int ConsistentProThr = Int32.Parse(rr.Count("宝贝相符", "3"));
        ConsistentProTotal = ConsistentProTotal + ConsistentProThr;

        int ConsistentProFor = Int32.Parse(rr.Count("宝贝相符", "4"));
        ConsistentProTotal = ConsistentProTotal + ConsistentProFor;

        int ConsistentProFiv = Int32.Parse(rr.Count("宝贝相符", "5"));
        ConsistentProTotal = ConsistentProTotal + ConsistentProFiv;
        double ConsistentProScore = (ConsistentProOne * 1 + ConsistentProTwo * 2 + ConsistentProThr * 3 + ConsistentProFor * 4 + ConsistentProFiv * 5) / ConsistentProTotal;

        int DeliverySpeedTotal = 0;
        int DeliverySpeedOne = Int32.Parse(rr.Count("发货速度", "1"));
        DeliverySpeedTotal = DeliverySpeedTotal + DeliverySpeedOne;

        int DeliverySpeedTwo = Int32.Parse(rr.Count("发货速度", "2"));
        DeliverySpeedTotal = DeliverySpeedTotal + DeliverySpeedTwo;

        int DeliverySpeedThr = Int32.Parse(rr.Count("发货速度", "3"));
        DeliverySpeedTotal = DeliverySpeedTotal + DeliverySpeedThr;

        int DeliverySpeedFor = Int32.Parse(rr.Count("发货速度", "4"));
        DeliverySpeedTotal = DeliverySpeedTotal + DeliverySpeedFor;

        int DeliverySpeedFiv = Int32.Parse(rr.Count("发货速度", "5"));
        DeliverySpeedTotal = DeliverySpeedTotal + DeliverySpeedFiv;
        double DeliverySpeedScore = (DeliverySpeedOne * 1 + DeliverySpeedTwo * 2 + DeliverySpeedThr * 3 + DeliverySpeedFor * 4 + DeliverySpeedFiv * 5) / DeliverySpeedTotal;

        this.Label1.Text = SellerAttiScore.ToString("0.00");
        this.Label2.Text = ConsistentProScore.ToString("0.00");
        this.Label3.Text = DeliverySpeedScore.ToString("0.00");

        Rating1.CurrentRating = Convert.ToInt32(SellerAttiScore);
        Rating1.ReadOnly = true;//不能再评价
        Rating2.CurrentRating = Convert.ToInt32(ConsistentProScore);
        Rating2.ReadOnly = true;//不能再评价
        Rating3.CurrentRating = Convert.ToInt32(DeliverySpeedScore);
        Rating3.ReadOnly = true;//不能再评价
        //ifRating(Rating1, Label1);
        //ifRating(Rating2, Label2);
        //ifRating(Rating3, Label3);
    }
    private void ifRating(Rating r, Label l)
    {
        r.CurrentRating = int.Parse(Session[r.ID].ToString());//显示评价结果
        l.Text = Session[r.ID].ToString();
        r.ReadOnly = true;//不能再评价
    }
    // 产品搜索
    protected void searchbutton_Click(object sender, EventArgs e)
    {
        String bookName = productkeyword.Text.ToString();
        String bookTypeName = "";
        if (this.smalltype.SelectedItem != null)
        {
            bookTypeName = this.smalltype.SelectedItem.ToString();
        }
        String beginPrice =  this.beginprice.Text.ToString();
        String endPrice =this.endprice.Text.ToString();

        String where = bookName + "|" + bookTypeName + "|" + beginPrice + "|" + endPrice;
        Application["where"] = where;
        Server.Transfer("productShow.aspx?url=productList.aspx");
        //Response.Redirect("productShow.aspx?url=productList.aspx?where="+Server.UrlEncode(where));
    }
    // 绑定大类名
    public void BindBigType()
    {
        try
        {
            DataSet ds = bookBLL.SelectBigType();
            if ((ds == null) ||
                (ds.Tables.Count == 0) ||
                (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
            {
                return;
            }
            else
            {
                this.bigtype.DataSource = ds;
                this.bigtype.DataTextField = "BigType_Namestr";
                this.bigtype.DataBind();          // 绑定大类
                String BigTypeName = this.bigtype.SelectedItem.Text.ToString();
                BindSmallType(BigTypeName);      // 绑定小类
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
    // 选择小类
    protected void smalltype_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }
    // 根据大类选择小类
    protected void bigtype_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            String BigTypeName = this.bigtype.SelectedItem.Text.ToString();
            BindSmallType(BigTypeName);
        }
        catch(Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
    // 绑定小类名
    protected void BindSmallType(String BigTypeName)
    {
        try
        {
            DataSet ds = bookBLL.SelectSmallType(BigTypeName);
            if ((ds == null) ||
                (ds.Tables.Count == 0) ||
                (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
            {
                smalltype.Items.Clear();
            }
            else
            {
                smalltype.Items.Clear();
                smalltype.DataSource = ds;
                smalltype.DataTextField = "SmallType_Namestr";
                smalltype.DataBind();
            }
            ListItem itemBlank = new ListItem();   //设置第一行默认为空
            itemBlank.Value = "";
            itemBlank.Text = "";
            if (!smalltype.Items.Contains(itemBlank))
            {
                smalltype.Items.Insert(0, itemBlank);
                smalltype.Items[0].Selected = true;
            }
            else
            {
                int index = smalltype.Items.IndexOf(itemBlank);
                smalltype.Items[index].Selected = true;
            }
        }
        catch (Exception ex)
        {
            smalltype.Items.Clear();
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
}
